package com.wokeMy.view.Lottery;

import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.method.MyApp;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class YingkTicketActivity extends BaseActivity implements View.OnLayoutChangeListener {

    private ImageView ticketback;
    private ImageView ticketleft;
    private ImageView ticketdelete;
    private TextView ticketnum,ticket_title;
    private TextView ticketmoney;
    private TextView zhu_ticket_tv,money_ticket_tv;
    private ImageView iconright;
    private ImageView ticketright;
    Button ticket_ok;
    String user_id;
    Dialog mydialog;
    ArrayList<String> blueBall;
    String orderString,orderissue,zhuTv,monTv,lotteryid,saletype,childtype;
    EditText beishubetet;
    String beishu,money;
    String type;//"和值","三同号","二同号","三不同号","二不同号"

    //Activity最外层的Layout视图
    private View activityRootView;
    //屏幕高度
    private int screenHeight = 0;
    //软件盘弹起后所占高度阀值
    private int keyHeight = 0;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_ticket);
    }

    @Override
    public void initView() {
        this.ticketright = (ImageView) findViewById(R.id.ticket_right);
        this.iconright = (ImageView) findViewById(R.id.icon_right);
        this.ticketmoney = (TextView) findViewById(R.id.ticket_money);
        this.ticketnum = (TextView) findViewById(R.id.ticket_num);
        this.beishubetet = (EditText) findViewById(R.id.beishu_bet_et);
        this.ticketdelete = (ImageView) findViewById(R.id.ticket_delete);
        this.ticketleft = (ImageView) findViewById(R.id.ticket_left);
        this.ticketback = (ImageView) findViewById(R.id.ticket_back);
        activityRootView = findViewById(R.id.root_layout);
        //获取屏幕高度
        screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
        //阀值设置为屏幕高度的1/3
        keyHeight = screenHeight/3;

        zhu_ticket_tv=myFindViewById(R.id.zhu_ticket_tv);
        money_ticket_tv=myFindViewById(R.id.money_ticket_tv);
        ticket_ok=myFindViewById(R.id.ticket_ok);
        ticket_title=myFindViewById(R.id.ticket_title);
    }

    @Override
    public void initData() {
        user_id=((MyApp)getApplication()).getDatas_load().getId();
        Intent intent = getIntent();
        blueBall=intent.getStringArrayListExtra("blueBall");
        orderissue=intent.getStringExtra("orderissue");
        zhuTv=intent.getStringExtra("zhuTv");
        monTv=intent.getStringExtra("monTv");
        lotteryid=intent.getStringExtra("lotteryid"); //玩法代码 118//双色球 106 大乐透 //123 银块3
        type=intent.getStringExtra("type");

    }

    @Override
    public void initActivity() {
        saletype="0";
        childtype="1";
        String title="确定投注";
        String momeyTv=type+" "+zhuTv+monTv;
        //type //"和值","三同号","二同号","三不同号","二不同号"
        String s;
        StringBuffer str=new StringBuffer();
        for (int i = 0; i < blueBall.size(); i++) {
            str.append(blueBall.get(i)+"*");
        }
        s = str.toString();
        s = s.substring(0,s.length() - 1);
        ticketnum.setText(s);
        orderString=s;
        switch (type) {
            case "和值":
                switch (lotteryid) {
                    case "123":
                        if (blueBall.size()>1){
                            saletype="1";
                        }
                        break;
                    default:
                        saletype="0";
                        break;
                }
                childtype="1";
                break;
            case "三同号":
                if (blueBall.get(0).length()>3){
                    blueBall.clear();
                    orderString="1*2*3*4*5*6";
                    saletype="0";
                }else {
                    saletype="1";
                        StringBuffer stringBuffer=new StringBuffer();
                        String mum=blueBall.get(0);
                        for (int i1 = 0; i1 < mum.length(); i1++) {
                            stringBuffer.append(mum.charAt(i1)+"*");
                        }
                        stringBuffer.deleteCharAt(stringBuffer.length()-1);
                    orderString=stringBuffer.toString();
                }

                childtype="2";
                break;
            case "三不同号":
                saletype="0";
                childtype="3";
                if (orderString.length()>6){
                    orderString="1*2*3*4*5*6";
                    saletype="0";
                    childtype="4";
                }
                break;
            case "二不同号":
                saletype="0";
                childtype="6";
                break;
            case "二同号":
                saletype="0";
                childtype="5";

                break;
            default:
                s="0";
                childtype="1";
                saletype="0";
                break;
        }
        ticket_title.setText(title);
        ticketmoney.setText(momeyTv);
        ticketback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //        String str="01 01 01 01 01 01 01 01 01 01<font color='#FF0000'> 02 02 02 </font>";

        zhu_ticket_tv.setText(zhuTv);
        money_ticket_tv.setText(monTv);
        ticket_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                beishu=beishubetet.getText().toString().trim();
                money=(Integer.parseInt(monTv.substring(0,monTv.length() - 1))*Integer.parseInt(beishu))+"";
                if (Integer.parseInt(money)>20000){
                    tishi("最高金额2万");
                }else {
                    mydialog= Util.createLoadingDialog(YingkTicketActivity.this);
                    doubleBet();
                }

            }
        });

        //添加layout大小发生改变监听器
        activityRootView.addOnLayoutChangeListener(this);
    }


//    private void getOrderNumString() {
//        ArrayList<String> chenckBall = blueBall;
//        StringBuffer stringBuffer =new StringBuffer();
//        StringBuffer sbRed =new StringBuffer();
//        for (int i = 0; i < chenckBall.size(); i++) {
//            String buleball;
//            int s = Integer.valueOf(chenckBall.get(i)) + 1;
//            if (s<10){
//                buleball="0"+s;
//            }else {
//                buleball=s+"";
//            }
//            stringBuffer.append(buleball);
//        }
//        chenckBall.clear();
//        for (int i = 0; i < chenckBall.size(); i++) {
//            String buleball;
//            int s = Integer.valueOf(chenckBall.get(i)) + 1;
//            if (s<10){
//                buleball="0"+s;
//            }else {
//                buleball=s+"";
//            }
//            sbRed.append(buleball);
//        }
//        orderString=stringBuffer.toString()+"|"+sbRed.toString();
//        Log.e("orderString",orderString);
//    }
    // 彩票双色球投注
    public void  doubleBet(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("lotteryid",lotteryid);//玩法代码 118//双色球 106 // 赢快3
        params.put("issue",orderissue); //指定查询的期号
        params.put("childtype",childtype);//子玩法代码 1和值 2三同号 3 三不同号 4三连号通选 5二同号 6二不同号复选
        params.put("saletype",saletype); //销售方式 0 单式 1复试 2胆拖
        params.put("lotterycode",orderString);//投注号码 101415262932|16
        params.put("appnumbers",beishu); //倍数
        params.put("lotterynumber",zhuTv.substring(0,zhuTv.length() - 1));//注数
        params.put("money",money);//投注金额
        Log.e("彩票双色球投注url",Constant.CPDOUBLE_URL);
        Log.e("彩票双色球投注params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.CPDOUBLE_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                closeLoadDial(mydialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("彩票双色球投注result", result);
                    try {
                        JSONObject jsonObject =new JSONObject(result);
                        if (jsonObject.get("flag").equals("success")) {
                            tishiToast("投注成功");
                            finish();
                        } else {
                            String info = jsonObject.getString("info");
                            if (jsonObject.has("data")){
                                info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                            }
                            tishi(info);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                closeLoadDial(mydialog);
            }
        });
    }

    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        //现在认为只要控件将Activity向上推的高度超过了1/3屏幕高，就认为软键盘弹起
        if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){

//            Toast.makeText(YingkTicketActivity.this, "监听到软键盘弹起...", Toast.LENGTH_SHORT).show();

        }else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){
            beishu=beishubetet.getText().toString().trim();
            if (beishu.equals("")||Integer.parseInt(beishu)<1){
                beishubetet.setText("1");
                Toast.makeText(YingkTicketActivity.this, "最小倍数注数为1", Toast.LENGTH_LONG).show();
            }else  if (Integer.parseInt(beishu)>999){
                beishubetet.setText("999");
                Toast.makeText(YingkTicketActivity.this, "最大倍数为999", Toast.LENGTH_LONG).show();
            }
            beishu=beishubetet.getText().toString().trim();
            money=(Integer.parseInt(monTv.substring(0,monTv.length() - 1))*Integer.parseInt(beishu))+"";
            money_ticket_tv.setText(money+"元");
//            Toast.makeText(YingkTicketActivity.this, "监听到软件盘关闭...", Toast.LENGTH_SHORT).show();

        }
    }
}
